package org.example.window;

import org.apache.flink.api.common.eventtime.TimestampAssignerSupplier;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.Arrays;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/2/23 09:36
 */
public class WindowIntervalJoinWithLateDemo {

    public static void main(String[] args) throws Exception{

        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        environment.setParallelism(1);

        WatermarkStrategy<Tuple2<String, Integer>> watermarkStrategy1 =
                WatermarkStrategy.<Tuple2<String, Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                .withTimestampAssigner((TimestampAssignerSupplier<Tuple2<String, Integer>>) context -> (element, recordTimestamp) -> element.f1 * 1000L);

        SingleOutputStreamOperator<Tuple2<String, Integer>> ds1 = environment.socketTextStream("127.0.0.1",7777)
                .map(value -> {
                    return Tuple2.of(value.split(",")[0], Integer.parseInt(value.split(",")[1]));
                })
                .returns(Types.TUPLE(Types.STRING,Types.INT))
                .assignTimestampsAndWatermarks(watermarkStrategy1);

        WatermarkStrategy<Tuple3<String, Integer,Integer>> watermarkStrategy2 =
                WatermarkStrategy.<Tuple3<String, Integer,Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                .withTimestampAssigner((TimestampAssignerSupplier<Tuple3<String, Integer,Integer>>) context -> (element, recordTimestamp) -> element.f1 * 1000L);

        SingleOutputStreamOperator<Tuple3<String, Integer, Integer>> ds2 = environment.socketTextStream("127.0.0.1",8888)
                .map(value -> {
                    String[] split = value.split(",");
                    return Tuple3.of(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]));
                })
                .returns(Types.TUPLE(Types.STRING,Types.INT,Types.INT))
                .assignTimestampsAndWatermarks(watermarkStrategy2);

        KeyedStream<Tuple2<String, Integer>, String> keyedStream1 = ds1.keyBy(value -> value.f0);
        KeyedStream<Tuple3<String, Integer,Integer>, String> keyedStream2 = ds2.keyBy(value -> value.f0);

        OutputTag<Tuple2<String, Integer>> tag1 = new OutputTag<>("tag1",Types.TUPLE(Types.STRING,Types.INT));
        OutputTag<Tuple3<String, Integer,Integer>> tag2 = new OutputTag<>("tag2",Types.TUPLE(Types.STRING,Types.INT,Types.INT));
        SingleOutputStreamOperator<String> streamOperator = keyedStream1
                .intervalJoin(keyedStream2)
                .between(Time.seconds(-2), Time.seconds(2))
                // 迟到数据侧输出流
                .sideOutputLeftLateData(tag1)
                .sideOutputRightLateData(tag2)
                .process(new ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>() {
                    /**
                     * 匹配的数据会执行这里
                     */
                    @Override
                    public void processElement(Tuple2<String, Integer> left, Tuple3<String, Integer, Integer> right,
                                               ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>.Context ctx,
                                               Collector<String> out) throws Exception {
                        out.collect(left + "--->" + right);
                    }
                });
        // 迟到的数据侧流输出
        streamOperator.getSideOutput(tag1).printToErr();
        streamOperator.getSideOutput(tag2).printToErr();

        streamOperator.print();

        environment.execute();

    }

}
